package c.w.taxi;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import c.w.connection.ConnDB;
import c.w.connection.DatabaseHepler;
import c.w.constants.PublicVariable;
import c.w.utils.ExitManager;
import c.w.utils.RegistCheck;

import com.www.taxi.R;

public class RegistActivity extends Activity {
	private Button loginB;
	private EditText editUserName, editUserPassword;
	private RegistCheck gc;
	private DatabaseHepler db;
	private ConnDB connDB;
	private PublicVariable publicValues;
	private ExitManager exitManager;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_regist);
		loginB = (Button)findViewById(R.id.registButton);
		//rebackB = (Button)findViewById(R.id.regist_reback_btn);
		editUserPassword = (EditText)findViewById(R.id.userPassword);
		editUserName = (EditText)findViewById(R.id.user_name);
		gc = new RegistCheck();
		connDB = new ConnDB();
		db  = new DatabaseHepler(RegistActivity.this, "user");
		
		loginB.setOnClickListener(new loginBListener());
		//rebackB.setOnClickListener(new rebackBListener());
		exitManager = new ExitManager();
		exitManager.getScreenManager().addStack(this);
		showToast();
	}

	/*@Override  菜单
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_regist, menu);
		return true;
	}*/
	
	class loginBListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			
			// TODO Auto-generated method stub
			String userPassword = editUserPassword.getText().toString();
			String userName = editUserName.getText().toString();
			publicValues.userPassword = userPassword;
			publicValues.userID = userName;
			
			if(gc.doCheck(userPassword)){
					//创建数据库存name , password
						connDB.doInsert(db, userName, userPassword);
						//意图机制实现页面的跳转
						Intent intent = new Intent(RegistActivity.this, MainActivity.class);
						startActivity(intent);
						//获得SDK的版本号
						int version =  Integer.valueOf(android.os.Build.VERSION.SDK);
						if(version > 5 ){
							//在进入下一个页面时，设置的动态效果
							overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
				}
			}
			else{
				AlertDialog.Builder ad = new AlertDialog.Builder(RegistActivity.this);
				ad.setMessage("输入错误，请重新输入！！");
				ad.setCancelable(true);
				AlertDialog alert =ad.create();
				alert.show();
			}
		}
		
	}
	class exitBListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			System.exit(0);
			//应该退出服务器的时候显示
			showExitToast();
			RegistActivity.this.finish();
		}
		
	}
	class rebackBListener implements OnClickListener{

		public void onClick(View v) {
			Intent intent = new Intent(RegistActivity.this, FirstLoginActivity.class);
			startActivity(intent);
			int version =  Integer.valueOf(android.os.Build.VERSION.SDK);
			if(version > 5 ){
				//在进入下一个页面时，设置的动态效果
				overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
			}
		}
		
	}
	private void showToast(){
		LayoutInflater li=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view=li.inflate(R.layout.toast,null);
		//把布局文件toast.xml转换成一个view
		Toast toast = new Toast(this);
		toast.setGravity(Gravity.BOTTOM, 0, 50);//设置toast的位置
		toast.setDuration(Toast.LENGTH_LONG);
		//载入toast.xml文件
		toast.setView(view);
		toast.show();
	}
	private void showExitToast(){
		LayoutInflater li=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view=li.inflate(R.layout.toast,null);
		//把布局文件toast.xml转换成一个view
		Toast toast = new Toast(this);
		toast.setView(view);
		//载入toast.xml文件
		toast.setText("正在退出程序");
		toast.setGravity(Gravity.BOTTOM, 0, 50);//设置toast的位置
		toast.setDuration(Toast.LENGTH_LONG);
		toast.show();
	}

}
